package com.xiaoyaoji.common.util;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;

@Data
@Builder
@Accessors(chain = true)
@FieldDefaults(level = AccessLevel.PRIVATE)
public class JsonResult<T> {

    private static final Integer CODE_SUCCESS = 1;
    private static final Integer CODE_FAIL = -1;
    private static final String STR_SUCCESS = "成功";
    private static final String STR_FAIL = "失败";

    /**
     * 是否成功
     */
    Boolean success;
    /**
     * 状态码
     */
    Integer statusCode;
    /**
     * 消息
     */
    String message;
    /**
     * 数据
     */
    @JSONField(serialzeFeatures = {SerializerFeature.WriteMapNullValue})
    T data;

    public static JsonResult success() {
        return JsonResult.success(null);
    }

    public static <T> JsonResult success(T data) {
        return JsonResult.success(STR_SUCCESS, data);
    }

    public static <T> JsonResult success(String message, T data) {
        return JsonResult.success(CODE_SUCCESS, message, data);
    }

    public static <T> JsonResult success(Integer code, String message, T data) {
        return JsonResult.builder().success(true).statusCode(code).message(message).data(data).build();
    }

    public static JsonResult fail() {
        return JsonResult.fail(null);
    }

    public static <T> JsonResult fail(T data) {
        return JsonResult.fail(STR_FAIL, data);
    }

    public static <T> JsonResult fail(String message, T data) {
        return JsonResult.fail(CODE_FAIL, message, data);
    }

    public static <T> JsonResult fail(Integer code, String message, T data) {
        return JsonResult.builder().success(false).statusCode(code).message(message).data(data).build();
    }

}
